const guestbookService = require('../../service/web/guestbook.js');

const guestbookController = {
    add: async(req,res) => {
        req.body.os = req.headers['user-agent'].split("(")[1].split(")")[0].split(";")[0]
        // 添加留言到当前访问的用户的留言板
        req.body.userID = req.targetUser._id
        const result = await guestbookService.add(req.body);
        res.send( {
            code: 200,
            message: 'success',
        })
    },
    getList: async(req,res) => {
        const { page,pageSize } = req.query;
        const left = (parseInt(page) - 1) * parseInt(pageSize)
        const right = parseInt(pageSize)
        // 从中间件获取当前访问的用户信息
        const userID = req.targetUser._id
        const { data,total } = await guestbookService.getList(left,right,userID);
        res.send({
            code: 200,
            message: '获取成功',
            data,
            total
        })
    },
    getBullet: async(req,res) => {
        // 从中间件获取当前访问的用户信息
        const userID = req.targetUser._id
        const result = await guestbookService.getBullet(userID);
        res.send({
            code: 200,
            message: '获取成功',
            data:result
        })
    }
}

module.exports = guestbookController;